/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.util;

import it.gashale.jacolib.core.JacolibError;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.net.URL;

public class FileSystem {
	static public String getJarDirectoryName(Class aClass) throws JacolibError {
		String classPath=aClass.getName().replace('.','/') + ".class";
		URL u = aClass.getClassLoader().getResource(classPath);
		if (!u.getProtocol().equals("jar")) return null;
		String s = u.getFile();
	    if (!s.startsWith("file:")) 
	    	throw new JacolibError("Jar file is not in the local file system");
		int end = s.indexOf("!");
		if (end==-1) throw new JacolibError("Bad jar URL");
	    String path = s.substring(5,end);
	    if (!path.endsWith(".jar"))
		throw new JacolibError("Jar file name does not end with .jar");
		path=path.replace('/',File.separatorChar);
		return new File(path).getParentFile().getPath();
	}
	
	static public File makeTempDirectory() throws JacolibError {
		File temp_dir=null;
		try{
			File dummyFile = File.createTempFile("Jacolib_","");
			if (!dummyFile.delete())
                            throw new JacolibError("Error creating temp directory. Could not delete dummy file");
			temp_dir = new File(dummyFile.getAbsolutePath());
			if (!temp_dir.mkdir())
                            throw new JacolibError("Error creating temp directory. Could not create temporary directory");
			temp_dir.deleteOnExit();
		} catch (IOException e){
			throw new JacolibError("Error creating temp directory. "+e);
		}
		return temp_dir;
	}
	
	static public String resourceToSting(String resource) throws IOException {
		InputStream in = FileSystem.class.getClassLoader().getResourceAsStream(resource);
		ByteArrayOutputStream out = new ByteArrayOutputStream (1024);
		byte [] aoBuffer = new byte [512];
		int nBytesRead;
		while ( (nBytesRead = in.read (aoBuffer)) > 0 ) {
			out.write (aoBuffer, 0, nBytesRead);
		}
		in.close ();
		return new String(out.toByteArray ());
	}
	static public String fileToSting(String filename) throws IOException {
		InputStream in = new FileInputStream(filename);
		ByteArrayOutputStream out = new ByteArrayOutputStream (1024);
		byte [] aoBuffer = new byte [512];
		int nBytesRead;
		while ( (nBytesRead = in.read (aoBuffer)) > 0 ) {
			out.write (aoBuffer, 0, nBytesRead);
		}
		in.close ();
		return new String(out.toByteArray ());
	}
}
